package com.example.j202411.views.Activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.j202411.databinding.ActivityBookDetailsBinding;
import com.example.j202411.Model.Books;
import com.example.j202411.utils.URLExtractor;
import com.google.gson.Gson;


import java.util.List;

public class BookDetailsActivity extends AppCompatActivity {
private ActivityBookDetailsBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityBookDetailsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Intent intent = getIntent() ;
        String getBook = intent.getStringExtra("bookData");
        Gson gson = new Gson() ;
        Books.ResultData.BookData book = gson.fromJson(getBook, Books.ResultData.BookData.class);
        Glide.with(this)
                .load(book.getImg())
                .into(binding.bookImg);
        binding.bookName.setText(book.getTitle());
        binding.bookReader.setText(book.getReading());
        binding.bookTime.setText(book.getBytime());
        binding.bookType.setText(book.getCatalog());
        binding.bookItr.setText(TextUtils.substring(book.getTags() + "\n" + book.getSub2(), 0,
                Math.min(50, (book.getTags() + "\n" + book.getSub2()).length())) + "...");
        binding.tvMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onMoreClicked(book.getSub2());
            }
        });

        String urlStr = book.getOnline();
        List<String> urls = URLExtractor.extractUrls(urlStr);
        String url1 = urls.get(0);
        String url2 = urls.get(1);
        String url3 = urls.get(2);
        binding.toJd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openWebsite(url1);
            }
        });
        binding.toDd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openWebsite(url2);
            }
        });
        binding.toSn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openWebsite(url3);
            }
        });
    }


    private void openWebsite(String uri) {
        String url = uri; // 替换成你要跳转的网址
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        } else {
            // 没有可以处理此Intent的应用程序
            Toast.makeText(this, "无法打开网页，请检查设备是否安装了浏览器", Toast.LENGTH_SHORT).show();
        }
    }

    //查看更多
    private void onMoreClicked(String bookItr2) {
        new AlertDialog.Builder(this)
                .setTitle("书籍详细介绍")
                .setMessage(bookItr2)
                .setPositiveButton(android.R.string.ok, null)
                .show();
    }
}